package com.example.qxfw.dataBaseManager.Controller;

import com.alibaba.excel.EasyExcel;
import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.*;
import com.example.qxfw.common.entity.CountryPoint;
import com.example.qxfw.common.entity.CountyPoint;
import com.example.qxfw.common.entity.yj.ImpGoal;
import com.example.qxfw.common.entity.yj.Village;
import com.example.qxfw.common.listener.GenericDataListener;
import com.example.qxfw.common.service.DangerSourceService;
import com.example.qxfw.common.service.FloodcontrolProjectService;
import com.example.qxfw.common.service.ImpGoalProService;
import com.example.qxfw.dataBaseManager.service.EmergencyResourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/7/7  星期五   14:05
 * @description : 此类用来 应急资源
 ***************************************/
@ApiIgnore
@Slf4j
@Api(tags = "数据库管理-应急资源")
@RestController
@CrossOrigin
@RequestMapping("/EmergencyResourcesController")
public class        EmergencyResourcesController {

    @Autowired
    private EmergencyResourcesService emergencyResourcesService;

    @Autowired
    private DangerSourceService dangerSourceService;

    @Autowired
    private ImpGoalProService impGoalProService;

    @Autowired
    private FloodcontrolProjectService floodcontrolProjectService;

    @ApiOperation("县点")
    @RequestMapping(value = "getAllCountyPoint", method = RequestMethod.GET)
    public SysResult getAllCountyPoint() {
        List<CountyPoint> allCountyPoint = this.emergencyResourcesService.getAllCountyPoint();
        return SysResult.success(allCountyPoint);
    }

    @ApiOperation("乡镇")
    @RequestMapping(value = "getAllCountryPoint", method = RequestMethod.GET)
    public SysResult getAllCountryPoint() {
        List<CountryPoint> allCountryPoint = this.emergencyResourcesService.getAllCountryPoint();
        return SysResult.success(allCountryPoint);
    }

    @ApiOperation("村庄")
    @RequestMapping(value = "getAllVillage", method = RequestMethod.GET)
    public SysResult getAllVillage() {
        List<Village> allVillage = this.emergencyResourcesService.getAllVillage();
        return SysResult.success(allVillage);
    }

    @ApiOperation("添加县点")
    @RequestMapping(value = "saveCountyPoint", method = RequestMethod.POST)
    public SysResult saveCountyPoint(@RequestBody CountyPoint countyPoint) {
        Integer county_point = this.emergencyResourcesService.selectJudge("county_point", countyPoint.getName());
        if (county_point > 0) {
            return SysResult.fail("该县点已存在");
        } else {
            Integer integer = this.emergencyResourcesService.saveCountyPoint(countyPoint);
            return SysResult.success(integer);
        }
    }

    @ApiOperation("添加乡镇")
    @RequestMapping(value = "saveCountryPoint", method = RequestMethod.POST)
    public SysResult saveCountryPoint(@RequestBody CountryPoint countryPoint) {
        Integer country_point = this.emergencyResourcesService.selectJudge("country_point", countryPoint.getName());
        if (country_point > 0) {
            return SysResult.fail("该乡镇已存在");
        } else {
            Integer integer = this.emergencyResourcesService.saveCountryPoint(countryPoint);
            return SysResult.success(integer);
        }
    }

    @ApiOperation("添加村庄")
    @RequestMapping(value = "saveVillage", method = RequestMethod.POST)
    public SysResult saveVillage(@RequestBody Village village) {
        Integer country_point = this.emergencyResourcesService.selectJudge("country_point", village.getVillagename());
        if (country_point > 0) {
            return SysResult.fail("该村庄已存在");
        } else {
            Integer integer = this.emergencyResourcesService.saveVillage(village);
            return SysResult.success(integer);
        }
    }

    @ApiOperation("更新县点")
    @RequestMapping(value = "updateCountyPoint", method = RequestMethod.POST)
    public SysResult updateCountyPoint(@RequestBody CountyPoint countyPoint){
        Integer integer = this.emergencyResourcesService.updateCountyPoint(countyPoint);
        return SysResult.success(integer);
    }

    @ApiOperation("更新乡镇")
    @RequestMapping(value = "updateCountryPoint", method = RequestMethod.POST)
    public SysResult updateCountryPoint(@RequestBody CountryPoint countryPoint){
        Integer integer = this.emergencyResourcesService.updateCountryPoint(countryPoint);
        return SysResult.success(integer);
    }

    @ApiOperation("更新村庄")
    @RequestMapping(value = "updateVillage",method = RequestMethod.POST)
    public SysResult updateVillage(@RequestBody Village village){
        Integer integer = this.emergencyResourcesService.updateVillage(village);
        return SysResult.success(integer);
    }

    @ApiOperation("删除县点")
    @RequestMapping(value = "deleteCountyPoint",method = RequestMethod.GET)
    public SysResult deleteCountyPoint(Integer id){
        Integer county_point = this.emergencyResourcesService.selectDelete("county_point", id);
        return SysResult.success(county_point);
    }

    @ApiOperation("删除乡镇")
    @RequestMapping(value = "deleteCountryPoint",method = RequestMethod.GET)
    public SysResult deleteCountryPoint(Integer id){
        Integer country_point = this.emergencyResourcesService.selectDelete("country_point", id);
        return SysResult.success(country_point);
    }

    @ApiOperation("删除村庄")
    @RequestMapping(value = "deleteVillage",method = RequestMethod.GET)
    public SysResult deleteVillage(Integer id){
        Integer village = this.emergencyResourcesService.selectDelete("village", id);
        return SysResult.success(village);
    }

    @ApiOperation("水库表的保存")
    @RequestMapping(value = "saveReservoir",method = RequestMethod.POST)
    public SysResult saveReservoir(@RequestBody Reservoir reservoir){
        Integer integer = this.emergencyResourcesService.saveReservoir(reservoir);
        return SysResult.success(integer);
    }

    @ApiOperation("水库表的更新")
    @RequestMapping(value = "updateReservoir",method = RequestMethod.POST)
    public SysResult updateReservoir(@RequestBody Reservoir reservoir){
        Integer integer = this.emergencyResourcesService.updateReservoir(reservoir);
        return SysResult.success(integer);
    }

    @ApiOperation("水库表删除")
    @RequestMapping(value = "deleteReservoir",method = RequestMethod.GET)
    public SysResult deleteReservoir(Integer id){
        Integer integer = this.emergencyResourcesService.deleteReservoir(id);
        return SysResult.success(integer);
    }

    @ApiOperation("根据ID排序查询水库表")
    @RequestMapping(value = "getReservoirOrderById",method = RequestMethod.GET)
    public SysResult getReservoirOrderById(){
        List<Reservoir> reservoirs = this.emergencyResourcesService.selectReservoirOrderById();
        return SysResult.success(reservoirs);
    }

    @ApiOperation("查询所有水库表")
    @RequestMapping(value = "getAllReservoir",method = RequestMethod.GET)
    public SysResult getAllReservoir(){
        List<Reservoir> allReservoir = this.emergencyResourcesService.getAllReservoir();
        return SysResult.success(allReservoir);
    }

    @ApiOperation("查询所有危险源")
    @RequestMapping(value = "getAllDangerSource",method = RequestMethod.GET)
    public SysResult getAllDangerSource(){
        List<DangerSource> allDangerSource = this.emergencyResourcesService.getAllDangerSource();
        return SysResult.success(allDangerSource);
    }

    @ApiOperation("查询所有重要目标")
    @RequestMapping(value = "getAllImpGoal",method = RequestMethod.GET)
    public SysResult getAllImpGoal(){
        List<ImpGoal> allImpGoal = this.emergencyResourcesService.getAllImpGoal();
        return SysResult.success(allImpGoal);
    }

    @ApiOperation("查询所有防汛设施")
    @RequestMapping(value = "getAllFloodControlProject",method = RequestMethod.GET)
    public SysResult getAllFloodControlProject(){
        List<FloodcontrolProject> allFloodControlProject = this.emergencyResourcesService.getAllFloodControlProject();
        return SysResult.success(allFloodControlProject);
    }

    @ApiOperation("查询所有地灾隐患点")
    @RequestMapping(value = "getAllPotentialPoints",method = RequestMethod.GET)
    public SysResult getAllPotentialPoints(){
        List<PotentialPoints> allPotentialPoints = this.emergencyResourcesService.getAllPotentialPoints();
        return SysResult.success(allPotentialPoints);
    }
    @ApiOperation("查询所有地灾类型")
    @RequestMapping(value = "getAllDisasterType",method = RequestMethod.GET)
    public SysResult getAllDisasterType(){
        List<DisasterType> allDisasterType = this.emergencyResourcesService.getAllDisasterType();
        return SysResult.success(allDisasterType);
    }

    @ApiOperation("添加危险源")
    @RequestMapping(value = "saveDangerSource",method = RequestMethod.POST)
    public SysResult saveDangerSource(@RequestBody DangerSource dangerSource){
        Integer integer = this.emergencyResourcesService.saveDangerSource(dangerSource);
        return SysResult.success(integer);
    }

    @ApiOperation("添加重要目标")
    @RequestMapping(value = "saveImpGoal",method = RequestMethod.POST)
    public SysResult saveImpGoal(@RequestBody ImpGoal impGoal){
        Integer integer = this.emergencyResourcesService.saveImpGoal(impGoal);
        return SysResult.success(integer);
    }
    @ApiOperation("添加防汛设施")
    @RequestMapping(value = "saveFloodControlProject",method = RequestMethod.POST)
    public SysResult saveFloodControlProject(@RequestBody FloodcontrolProject floodcontrolProject){
        Integer integer = this.emergencyResourcesService.saveFloodControlProject(floodcontrolProject);
        return SysResult.success(integer);
    }

    @ApiOperation("更新危险源")
    @RequestMapping(value = "updateDangerSource",method = RequestMethod.POST)
    public SysResult updateDangerSource(@RequestBody DangerSource dangerSource){
        Integer integer = this.emergencyResourcesService.updateDangerSource(dangerSource);
        return SysResult.success(integer);
    }

    @ApiOperation("更新重要目标")
    @RequestMapping(value = "updateImpGoal",method = RequestMethod.POST)
    public SysResult updateImpGoal(@RequestBody ImpGoal impGoal){
        Integer integer = this.emergencyResourcesService.updateImpGoal(impGoal);
        return SysResult.success(integer);
    }

    @ApiOperation("更新防汛设施")
    @RequestMapping(value = "updateFloodControlProject",method = RequestMethod.POST)
    public SysResult updateFloodControlProject(@RequestBody FloodcontrolProject floodcontrolProject){
        Integer integer = this.emergencyResourcesService.updateFloodControlProject(floodcontrolProject);
        return SysResult.success(integer);
    }

    @ApiOperation("删除危险源")
    @RequestMapping(value = "deleteDangerSource",method = RequestMethod.GET)
    public SysResult deleteDangerSource(Integer id){
        Integer integer = this.emergencyResourcesService.deleteDangerSource(id);
        return SysResult.success(integer);
    }
    @ApiOperation("删除重要目标")
    @RequestMapping(value = "deleteImpGoal",method = RequestMethod.GET)
    public SysResult deleteImpGoal(Integer id){
        Integer integer = this.emergencyResourcesService.deleteImpGoal(id);
        return SysResult.success(integer);
    }

    @ApiOperation("删除防汛设施")
    @RequestMapping(value = "deleteFloodControlProject",method = RequestMethod.GET)
    public SysResult deleteFloodControlProject(Integer id){
        Integer integer = this.emergencyResourcesService.deleteFloodControlProject(id);
        return SysResult.success(integer);
    }

    @PostMapping("/importDangerSource")
    public SysResult importDangerSource(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), DangerSource.class, new GenericDataListener(dangerSourceService)).sheet().doRead();
        return SysResult.success("success");
    }

    @PostMapping("/importImpGoal")
    public SysResult importImpGoal(MultipartFile file) throws IOException{
        EasyExcel.read(file.getInputStream(), ImpGoal.class, new GenericDataListener(impGoalProService)).sheet().doRead();
        return SysResult.success("success");
    }

    @RequestMapping(value = "importFloodControlProject",method = RequestMethod.POST)
    public SysResult importFloodControlProject(MultipartFile file) throws IOException{
        EasyExcel.read(file.getInputStream(), FloodcontrolProject.class, new GenericDataListener(floodcontrolProjectService)).sheet().doRead();
        return SysResult.success("success");
    }

}
